package com.alieneyesolutions.shakepass;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class ShakePass extends Activity implements SensorEventListener {

	String 		lower_alpha = "abcdefghijklmnopqrstuvwxyz";	
	String 		upper_alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	String 		numeric = "0123456789";
	String 		punctuation = ";[]:?=+-.,@#$%&*()";
	Button 		btn_generate;
	TextView	lbl_result;
	CheckBox	chk_inc_punctuation;
	CheckBox	chk_inc_numbers;
	CheckBox	chk_inc_mixed_case;
	CheckBox	chk_inc_letters;
	EditText	txt_length;
	private float mLastX, mLastY, mLastZ;
	private long lastUpdate = -1;
	private static final int SHAKE_THRESHOLD = 400;
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake_pass);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_shake_pass, menu);
        return true;
    }
    
    // ACCELEROMETER FUNCTIONS
    
    public void onResume() {
    	super.onResume();
    	mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
 
    protected void onPause() {
    	super.onPause();
    	mSensorManager.unregisterListener(this);
   	}

   	public void onAccuracyChanged(Sensor sensor, int accuracy) {
    	// can be safely ignored for this demo
   	}
   	
   	public void onSensorChanged(SensorEvent event) {
   		lbl_result = (TextView) findViewById(R.id.lbl_result);
   		
   		long curTime = System.currentTimeMillis();
   		
   		// only allow one update every 100ms.
	    if ((curTime - lastUpdate) > 100) {
			long diffTime = (curTime - lastUpdate);
			lastUpdate = curTime;
	   		float x = event.values[0];
	   		float y = event.values[1];
	   		float z = event.values[2];
	   		float speed = Math.abs(x+y+z - mLastX - mLastY - mLastZ) / diffTime * 10000;
	   		
	   		if (speed > SHAKE_THRESHOLD) {
	   			//lbl_result.setText(generatePassword(numeric)+"");
	   			lbl_result.setText(generatePassword(getCharArray())+"");
	   		}
	   		mLastX = x;
	   		mLastY = y;
	   		mLastZ = z;
	    }
	}
    
    
    // CUSTOM FUNCTIONS
   	
   	private String getCharArray(){
   		
   		String ret_val = "";
   		
   		chk_inc_punctuation = (CheckBox) findViewById(R.id.chk_inc_punctuation);
   		chk_inc_numbers = (CheckBox) findViewById(R.id.chk_inc_numbers);
   		chk_inc_mixed_case = (CheckBox) findViewById(R.id.chk_inc_mixed_case);
   		chk_inc_letters = (CheckBox) findViewById(R.id.chk_inc_letters);
   		
   		if(chk_inc_letters.isChecked() || ret_val == "")
   			ret_val += lower_alpha;
   		if(chk_inc_mixed_case.isChecked())
   			ret_val += upper_alpha;
   		if(chk_inc_numbers.isChecked())
   			ret_val += numeric;
   		if(chk_inc_punctuation.isChecked())
   			ret_val += punctuation;
   		   		
   		return ret_val;
   		
   	}
    
    public String generatePassword(String str) {
    	txt_length = (EditText) findViewById(R.id.txt_length);
    	Random random = new Random();
    	String password = "";
    	for( int x=1 ; x <= Integer.parseInt(txt_length.getText().toString()); x++ ) {
    		password += str.charAt(getRandomItem(str, random));
    	}
    	return password;
    }
    
    public int getRandomItem(String str, Random aRandom){
    	if ( 0 > str.length() ) {
    	      throw new IllegalArgumentException("Start cannot exceed End.");
    	}
    	long range = (long)str.length() - (long)1;
    	long fraction = (long)(range * aRandom.nextDouble());
    	int randomNumber =  (int)(fraction);    
    	return randomNumber;
    }

}
